package cn.tedu.weibo2.controller;

import cn.tedu.weibo2.pojo.dto.WeiboDTO;
import cn.tedu.weibo2.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo2.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo2.response.JsonResult;
import cn.tedu.weibo2.service.WeiboService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboService weiboService;

    @PostMapping("insert")
    public JsonResult insert(WeiboDTO weiboDTO){
      log.debug("微博参数:{}",weiboDTO);
      weiboService.insert(weiboDTO);
      return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult list(){
        List<WeiboIndexVO>  weiboIndexVOList =weiboService.list();
        log.debug("查询结果:{}",weiboIndexVOList);
        return JsonResult.ok(weiboIndexVOList);
    }
    @GetMapping("select")
    public JsonResult select(Long id){
        log.debug("用户id:{}",id);
        List<WeiboDetailVO>weiboDetailVOList=weiboService.selectByIndex(id);
        log.debug("返回结果:{}",weiboDetailVOList);
        return JsonResult.ok(weiboDetailVOList);
    }

    @DeleteMapping("delete/{weiboId}")
    public JsonResult delete(@PathVariable("weiboId") Long id){
        log.debug("删除的微博id:{}",id);
        weiboService.deleteById(id);
        return JsonResult.ok();
    }
    @GetMapping("detail/{id}")
    public JsonResult weiboDetail(@PathVariable("id") Long weiboId){
        log.debug("查询微博详情的id:{}",weiboId);
        WeiboDetailVO weiboDetailVO=weiboService.detailWeibo(weiboId);
        log.debug("查询的微博详情页:{}",weiboDetailVO);
        return JsonResult.ok(weiboDetailVO);
    }
}
